//
//  ChatMessageEditViewController.m
//  Btpin
//
//  Created by Vic on 2020/3/22.
//  Copyright © 2020 Vic. All rights reserved.
//

#import "ChatMessageEditViewController.h"

@interface ChatMessageEditViewController ()<UITextFieldDelegate, UITextViewDelegate>
{
    PSTextView *contentTextView;
}
@end

@implementation ChatMessageEditViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (contentTextView.textView.isFirstResponder) {
        [contentTextView.textView resignFirstResponder];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = @"常用语";
    self.view.backgroundColor = THEMECOLOR_OF_WHITE;
    
    contentTextView = [[PSTextView alloc] initWithFrame:CGRectMake(ScalePS(20), ScalePS(30), WIDTH-40*Scale, ScalePS(200)) leftText:@"" text:self.isEditFlag?self.editModel.text:nil textColor:THEMECOLOR_OF_TEXT_333333 placeholder:@"输入常用语" font:Font_15 editable:YES limitMax:LimitText_Max_Number target:self showNumber:YES];
    contentTextView.textView.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:contentTextView];
    contentTextView.backgroundColor = COLOR_OF_WHITE;
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, 50, 44) imageName:nil target:self action:@selector(okBtnTap) title:@"完成" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)okBtnTap {
    if (contentTextView.textView.text && contentTextView.textView.text.length) {
        if (contentTextView.textView.text.length>=10) {
            [self submitData];
        } else {
            SVPHUDError(@"常用语不小于10个字");
            [contentTextView.textView becomeFirstResponder];
        }
    } else {
        SVPHUDError(@"请输入常用语");
        [contentTextView.textView becomeFirstResponder];
    }
}
- (void)submitData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setValue:contentTextView.textView.text forKey:@"text"];
    [BaseNetManager requestWithMethod:POST path:self.isEditFlag?Chat_EditSentence_URL:Chat_AddSentence_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(@"成功");
            
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_ChatMessage_OK object:nil];
            
            if (self.edit) {
                self.edit(YES);
            }
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (contentTextView.textView.isFirstResponder) {
        [contentTextView.textView resignFirstResponder];
    }
}

@end
